package com.baitiaojun.moments.timer;

import com.baitiaojun.api.domain.moments.UserMomentsStatus;
import com.baitiaojun.api.util.ConstantUtil;
import com.baitiaojun.moments.mapper.UserMomentsStatusMapper;
import com.baitiaojun.moments.service.VideoMomentsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @类名 MomentsStatusTimer
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/1/30 17:40
 * @版本 1.0
 */
@Slf4j
@Component
public class VideoMomentsStatusTimer {

    @Resource
    private UserMomentsStatusMapper momentsStatusMapper;

    @Autowired
    private VideoMomentsService videoMomentsService;

    @Scheduled(initialDelay = 3, fixedRate = 3000)
    public void videoMomentsProcess() throws Exception {
        log.info("=========>重新尝试动态添加");
        List<UserMomentsStatus> momentsStatusList = momentsStatusMapper.selectMomentsStatus();
        for (UserMomentsStatus momentsStatus : momentsStatusList) {
            String status = momentsStatus.getStatus();
            if (ConstantUtil.FAIL_STATUS.equals(status) && momentsStatus.getTrycount() < 2) {
                Long videoId = momentsStatus.getContentid();
                Long userId = momentsStatus.getUserid();
                if (ObjectUtils.isNotEmpty(videoId) && ObjectUtils.isNotEmpty(userId)) {
                    videoMomentsService.publishVideoMoments(videoId, userId);
                }
            }
        }
    }
}
